package com.app.micaihu.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.ad.AdConfig;
import com.app.micaihu.ad.toutiao.ToutiaoAdManager;
import com.app.micaihu.ad.toutiao.ToutiaoCloseDialog;
import com.app.micaihu.ad.toutiao.ToutiaoNativeAd;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.bean.ad.NativeAd;
import com.app.micaihu.bean.game.GameCenterEntity;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.bean.news.ViewGameVideoFour;
import com.app.micaihu.bean.news.ViewGameVideoOne;
import com.app.micaihu.bean.news.ViewGameVideoThree;
import com.app.micaihu.bean.news.ViewGameVideoTwo;
import com.app.micaihu.bean.news.ViewNewsBigImg;
import com.app.micaihu.bean.news.ViewNewsNoImg;
import com.app.micaihu.bean.news.ViewNewsSmallImg;
import com.app.micaihu.bean.news.ViewNewsThreeImg;
import com.app.micaihu.bean.news.ViewNewsTopicBig;
import com.app.micaihu.bean.news.ViewNewsTopicSmall;
import com.app.micaihu.bean.news.ViewPicBigImg;
import com.app.micaihu.bean.news.ViewToutiaoAd;
import com.app.micaihu.bean.news.ViewVideoBigImg;
import com.app.micaihu.bean.news.ViewVideoBigImgOne;
import com.app.micaihu.bean.news.ViewVideoSmallImg;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.db.NewsDBSqlHelper;
import com.app.micaihu.db.NewsDBUtil;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.main.game.GameDetailActivity;
import com.app.micaihu.view.main.game.view.RanklGameView;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.LogUtils;
import com.bytedance.sdk.openadsdk.FilterWord;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻列表的适配器
 *
 * @author linkai
 */
public class NewsAdapter extends AppBaseAdapter<NewsEntity> {

    private static final int NEWS_NO_IMAGE = 0;// 文章没有图片
    private static final int NEWS_ONE_IMAGE = 1;// 文章一个图片
    private static final int NEWS_THREE_IMAGE = 2;// 文章三个图片
    private static final int NEWS_BIG_IMAGE = 3;// 文章大图(文章  图集)
    private static final int PIC_BIG_IMAGE = 4;// 图片大图（图集  视频）
    private static final int VIDEO_ONE_IMAGE = 5;// 视频一张图
    private static final int VIDEO_BIG_IMAGE = 6;// 视频一张图大图（视频列表样式）
    private static final int VIDEO_BIG_IMAGE_ONE = 7;// 视频一张图大图（信息流中的视频）
    public static final int FLUSH = 8;// 刷新样式
    private static final int AD_SMALL_BAIDU = 9;//百度小广告类型
    private static final int AD_BIG_BAIDU = 10;//百度大广告类型
    private static final int AD_BIG_GDT = 11;//广电通大广告类型
    private static final int AD_SMALL_GDT = 12;//广点通小广告类型
    private static final int GAMEN_VIDEO_ONE = 14;//游戏视频类型1
    private static final int GAMEN_VIDEO_TWO = 15;//游戏视频类型2
    private static final int GAMEN_VIDEO_THREE = 16;//游戏视频类型3(信息流中的无视频按钮视频)
    private static final int GAMEN_VIDEO_FOUR = 17;//游戏视频类型 (图库中的视频)
    private static final int GAMEN_PIC_NEWS = 18;//游戏图片直接下载,适配文章列表
    private static final int GAMEN_PIC_PIC = 19;//游戏图片直接下载,适配图库列表
    private static final int GAMEN_PIC_VIDEO = 20;//游戏图片直接下载,适配视频列表
    private static final int TOPIC_BIG = 21;//话题大图
    private static final int TOPIC_SMALL = 22;//话题小图
    private static final int NEWS_THREE_IMAGE_WITH_NUM = 23;//三张图带图片张数
    private static final int GAME_RANKING_VIEW = 24;//游戏排行
    private static final int AD_TT_SMALL = 25;  //头条小图
    private static final int AD_TT_BIG = 26; //头条大图

    private static final int DEFAULT = 13;//没有匹配到类型

    private static List<String> readList;//阅读集合
    private String type;//当前适配的栏目类型
    private List<GameCenterEntity> rankList;
    private String mRankName = "";
    private String channelName = "";

    public void setRankList(List<GameCenterEntity> rankList) {
        this.rankList = rankList;
    }


    public NewsAdapter(List<NewsEntity> list, Context context, String type) {
        super(list, context);
        this.type = type;
        if (readList == null) {
            String sql = "select * from " + NewsDBSqlHelper.NEWS_SEE;
            readList = NewsDBUtil.getInstance().getListID(sql);
        }

    }


    public NewsAdapter(List<NewsEntity> list, Context context, String type, String channelName, List<GameCenterEntity> rankList, String rankName) {
        super(list, context);
        this.type = type;
        this.rankList = rankList;
        this.mRankName = rankName;
        this.channelName = channelName;
        if (readList == null) {
            String sql = "select * from " + NewsDBSqlHelper.NEWS_SEE;
            readList = NewsDBUtil.getInstance().getListID(sql);
        }
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        // 获取position对应的数据
        NewsEntity news = list.get(position);
        if (news == null) {
            news = new NewsEntity();
        }
        //首先判断该数据是否为 广告
        if (news.getNativeAd() != null && !TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_H5)
                && !TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_TOUTIAO)) {
            View view = AdConfig.getNativeAdView(context, type, news.getNativeAd(), position, convertView, UmEventId.INSTANCE.getTuijian());
//            View view = AdConfig.getNativeAdView(context, type, news.getNativeAd(), position, convertView, mBaiduAdCache);
            if (view != null && view.getTag() != null) {
                return view;
            } else {
                return inflater.inflate(R.layout.item_layout_gone, null);
            }
        }
        int type = getItemViewType(position);
        if (type == NEWS_NO_IMAGE || type == NEWS_ONE_IMAGE || type == NEWS_THREE_IMAGE || type == NEWS_BIG_IMAGE) {
            if ("推荐".equals(channelName)) {
                MobclickAgentUtils.event(UmEventId.articlelist_exposed, UmEventId.INSTANCE.getRecommend());
            } else if ("热点".equals(channelName)) {
                MobclickAgentUtils.event(UmEventId.articlelist_exposed, UmEventId.INSTANCE.getHot());
            }
        } else if (type == VIDEO_ONE_IMAGE || type == VIDEO_BIG_IMAGE || type == VIDEO_BIG_IMAGE_ONE) {
            if ("推荐".equals(channelName)) {
                MobclickAgentUtils.event(UmEventId.videolist_exposed, UmEventId.INSTANCE.getRecommend());
            } else if ("热点".equals(channelName)) {
                MobclickAgentUtils.event(UmEventId.videolist_exposed, UmEventId.INSTANCE.getHot());
            }
        } else if (type == GAMEN_VIDEO_ONE || type == GAMEN_VIDEO_TWO || type == GAMEN_VIDEO_THREE ||
                type == GAMEN_VIDEO_FOUR || type == GAMEN_PIC_NEWS || type == GAMEN_PIC_PIC || type == GAMEN_PIC_VIDEO) {
            if ("推荐".equals(channelName)) {
                MobclickAgentUtils.event(UmEventId.game_exposed, UmEventId.INSTANCE.getTuijian());
            }
        }

        if (type == AD_TT_BIG) {
            LogUtils.e("toutiao","AD_TT_BIG------------------");
            return getTouTiaoView(convertView, parent, position);
        }
        if (convertView == null) {
            switch (type) {
                case GAME_RANKING_VIEW:
                    convertView = inflater.inflate(R.layout.layout_game_center_ranklist, null);
                    ViewHolder1 viewHolder1 = new ViewHolder1(convertView);
                    viewHolder1.loaddata(rankList);
                    convertView.setTag(viewHolder1);
                    break;
                case NEWS_THREE_IMAGE_WITH_NUM:
                case NEWS_THREE_IMAGE://三张图
                    convertView = inflater.inflate(R.layout.item_newslist_three, null);
                    ViewNewsThreeImg viewNewsThreeImgWitPicNum = new ViewNewsThreeImg(convertView);
                    viewNewsThreeImgWitPicNum.loadData(news);
                    break;
                case NEWS_NO_IMAGE://没有图
                    convertView = inflater.inflate(R.layout.item_newslist_no_image, null);
                    ViewNewsNoImg viewNewsNoImg = new ViewNewsNoImg(convertView);
                    viewNewsNoImg.loadData(news);
                    break;
                case NEWS_ONE_IMAGE://一张图
                    convertView = inflater.inflate(R.layout.item_newslist_normal, null);
                    ViewNewsSmallImg viewNewsSmallImg = new ViewNewsSmallImg(convertView);
                    viewNewsSmallImg.loadData(news);
                    break;
                case NEWS_BIG_IMAGE://文章大图(适配文章 图集 视频的大图)
                    convertView = inflater.inflate(R.layout.item_newslist_big_news_image, null);
                    ViewNewsBigImg viewNewsBigImg = new ViewNewsBigImg(convertView);
                    viewNewsBigImg.loadData(news);
                    break;
                case PIC_BIG_IMAGE://图集大图（适配图集列表）
                    convertView = inflater.inflate(R.layout.item_newslist_big_pic_img, null);
                    ViewPicBigImg viewPicBigImg = new ViewPicBigImg(convertView);
                    viewPicBigImg.loadData(news);
                    break;
                case VIDEO_BIG_IMAGE://视频大图（适配视频列表）
                    convertView = inflater.inflate(R.layout.item_newslist_big_video_img1, null);
                    ViewVideoBigImg viewVideoBigImg = new ViewVideoBigImg(convertView);
                    viewVideoBigImg.loadData(news);
                    break;
                case VIDEO_BIG_IMAGE_ONE://视频大图（信息流列表视频）
                    convertView = inflater.inflate(R.layout.item_newslist_big_video_img_one, null);
                    ViewVideoBigImgOne viewVideoBigImgOne = new ViewVideoBigImgOne(convertView);
                    viewVideoBigImgOne.loadData(news);
                    break;
                case VIDEO_ONE_IMAGE://小图视频
                    convertView = inflater.inflate(R.layout.item_newslist_video_normal, null);
                    ViewVideoSmallImg viewVideoSmallImg = new ViewVideoSmallImg(convertView);
                    viewVideoSmallImg.loadData(news);
                    break;
                case GAMEN_VIDEO_ONE://游戏视频下载（信息流列表视频）
                    convertView = inflater.inflate(R.layout.ad_game_video_one, null);
                    ViewGameVideoOne viewGameVideoOne = new ViewGameVideoOne(convertView);
                    viewGameVideoOne.loadData(news);
                    break;
                case GAMEN_VIDEO_TWO://游戏视频下载（视频列表视频）
                    convertView = inflater.inflate(R.layout.ad_game_video_two, null);
                    ViewGameVideoTwo viewGameVideoTwo = new ViewGameVideoTwo(convertView);
                    viewGameVideoTwo.loadData(news);
                    break;
                case GAMEN_VIDEO_THREE://游戏下载（信息流列表图片视频,能跳视频内页）
                    convertView = inflater.inflate(R.layout.ad_game_video_three, null);
                    ViewGameVideoThree viewGameVideoThree = new ViewGameVideoThree(convertView);
                    viewGameVideoThree.loadData(news);
                    break;
                case GAMEN_VIDEO_FOUR://游戏（图库列表图片视频,能跳视频内页）
                    convertView = inflater.inflate(R.layout.ad_game_img_picchannel, null);
                    ViewGameVideoFour viewGameVideoFour = new ViewGameVideoFour(convertView);
                    viewGameVideoFour.loadData(news);
                    break;
                case TOPIC_SMALL://话题小图
                    convertView = inflater.inflate(R.layout.item_newslist_topic_small, null);
                    ViewNewsTopicSmall viewNewsTopicSmall = new ViewNewsTopicSmall(convertView);
                    viewNewsTopicSmall.loadData(news);
                    break;
                case TOPIC_BIG://话题大图
                    convertView = inflater.inflate(R.layout.item_newslist_topic_big, null);
                    ViewNewsTopicBig viewNewsTopicBig = new ViewNewsTopicBig(convertView);
                    viewNewsTopicBig.loadData(news);
                    break;
                case FLUSH://刷新
                    convertView = inflater.inflate(R.layout.item_newslist_flush, null);
                    if (TextUtils.equals(this.type, TypeConstant.CHANNEL_TYPE_PICS) ||
                            TextUtils.equals(this.type, TypeConstant.CHANNEL_TYPE_VIDEO)) {
                        convertView.setPadding(0, 0, 0, 0);
                        convertView.setBackgroundResource(R.drawable.item_newslist_flush_bg1_selector);
                    }
                case AD_TT_BIG://头条
                    convertView = inflater.inflate(R.layout.item_toutiao_express_ad, null);
                    ViewToutiaoAd viewToutiaoAd = new ViewToutiaoAd(convertView);
                    viewToutiaoAd.loadData(news, true);
                    break;
                default:
                    convertView = inflater.inflate(R.layout.item_layout_gone, null);
                    break;
            }
        } else {
            switch (type) {
                case GAME_RANKING_VIEW:
                    ViewHolder1 viewHolder1 = (ViewHolder1) convertView.getTag();
                    viewHolder1.loaddata(rankList);
                    break;
                case NEWS_THREE_IMAGE_WITH_NUM:
                case NEWS_THREE_IMAGE:
                    ViewNewsThreeImg viewNewsThreeImgWithPicNum = (ViewNewsThreeImg) convertView.getTag();
                    viewNewsThreeImgWithPicNum.loadData(news);
                    break;
                case NEWS_NO_IMAGE:
                    ViewNewsNoImg viewNewsNoImg = (ViewNewsNoImg) convertView.getTag();
                    viewNewsNoImg.loadData(news);
                    break;
                case NEWS_ONE_IMAGE:
                    ViewNewsSmallImg viewNewsSmallImg = (ViewNewsSmallImg) convertView.getTag();
                    viewNewsSmallImg.loadData(news);
                    break;
                case NEWS_BIG_IMAGE:
                    ViewNewsBigImg viewNewsBigImg = (ViewNewsBigImg) convertView.getTag();
                    viewNewsBigImg.loadData(news);
                    break;
                case PIC_BIG_IMAGE:
                    ViewPicBigImg viewPicBigImg = (ViewPicBigImg) convertView.getTag();
                    viewPicBigImg.loadData(news);
                    break;
                case VIDEO_BIG_IMAGE:
                    ViewVideoBigImg viewVideoBigImg = (ViewVideoBigImg) convertView.getTag();
                    viewVideoBigImg.loadData(news);
                    break;
                case VIDEO_BIG_IMAGE_ONE:
                    ViewVideoBigImgOne viewVideoBigImgOne = (ViewVideoBigImgOne) convertView.getTag();
                    viewVideoBigImgOne.loadData(news);
                    break;
                case VIDEO_ONE_IMAGE:
                    ViewVideoSmallImg viewVideoSmallImg = (ViewVideoSmallImg) convertView.getTag();
                    viewVideoSmallImg.loadData(news);
                    break;
                case GAMEN_VIDEO_ONE://游戏视频下载（信息流列表视频）
                    ViewGameVideoOne viewGameVideoOne = (ViewGameVideoOne) convertView.getTag();
                    viewGameVideoOne.loadData(news);
                    break;
                case GAMEN_VIDEO_TWO://游戏视频下载（视频列表视频）
                    ViewGameVideoTwo viewGameVideoTwo = (ViewGameVideoTwo) convertView.getTag();
                    viewGameVideoTwo.loadData(news);
                    break;
                case GAMEN_VIDEO_THREE://游戏视频下载（信息流列表图片视频,能跳视频内页）
                    ViewGameVideoThree viewGameVideoThree = (ViewGameVideoThree) convertView.getTag();
                    viewGameVideoThree.loadData(news);
                    break;
                case GAMEN_VIDEO_FOUR:
                    ViewGameVideoFour viewGameVideoFour = (ViewGameVideoFour) convertView.getTag();
                    viewGameVideoFour.loadData(news);
                    break;
                case TOPIC_SMALL:
                    ViewNewsTopicSmall viewNewsTopicSmall = (ViewNewsTopicSmall) convertView.getTag();
                    viewNewsTopicSmall.loadData(news);
                    break;
                case TOPIC_BIG:
                    ViewNewsTopicBig viewNewsTopicBig = (ViewNewsTopicBig) convertView.getTag();
                    viewNewsTopicBig.loadData(news);
                    break;
                case FLUSH:
                    //不需要任何操作
                    break;
                case AD_TT_BIG://头条
                    ViewToutiaoAd viewToutiaoAd = (ViewToutiaoAd) convertView.getTag();
                    viewToutiaoAd.loadData(news, false);
                    break;
                default:
                    //不需要任何操作
//                    convertView = inflater.inflate(R.layout.item_layout_gone,null);
                    break;
            }
        }
        return convertView;
    }


    @Override
    public int getViewTypeCount() {
        return 27;
    }

    /**
     * 得到UI类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        NewsEntity news = list.get(position);
        if (news == null) {
            return DEFAULT;
        }
        // 文章类型(1.普通文章、2.普通图集、3普通视频 4.电商文章 5.网页文章---只显示1,2,3,10
        // 6.电商网页软文 7.网页视频 8.轻专题 9.广告)
        String articleType = news.getArticleType();
        if (TextUtils.isEmpty(articleType)) {
            return DEFAULT;
        }
        switch (articleType) {//这里出现了空指针
            //适配所有文章
            case TypeConstant.NEWS_NORMAL:
            case TypeConstant.NEWS_SHOP:
            case TypeConstant.NEWS_WEB:
                StatService.onEvent(context, "article_exposed", "信息流_文章", 1);// 统计的点击的次数
            case TypeConstant.NEWS_SHOP_WEB:
                //适配新闻专题
            case TypeConstant.NEWS_TOPIC:
            case TypeConstant.GAME_RAIDERS_TEXT://游戏图文攻略
            case TypeConstant.NEWS_MARKET://电商
            case TypeConstant.GAME_RAIDERS_VIDEO://游戏视频攻略
            case TypeConstant.EXCLUSIVE_NEWS_BIG_IMAGE://
            {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                    //小图
                    if (news.getArticleThumb().length >= 1 && news.getArticleThumb().length < 3) {
                        return NEWS_ONE_IMAGE;
                    } else if (news.getArticleThumb().length >= 3) {
                        return NEWS_THREE_IMAGE;
                    } else {
                        return NEWS_NO_IMAGE;
                    }
                } else {
                    //大图
                    return NEWS_BIG_IMAGE;
                }
            }
            //适配所有图集
            case TypeConstant.PIC_NORMAL:
            case TypeConstant.PIC_TOPIC: {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                    return NEWS_ONE_IMAGE;
                } else if (TypeConstant.CHANNEL_TYPE_PICS.equals(this.type)
                        && TypeConstant.NEWS_UI_BIG_TWO_IMG.equals(news.getUiType())) {
                    return PIC_BIG_IMAGE;
                } else {
                    return NEWS_BIG_IMAGE;
                }
            }

            //图库商品
            case TypeConstant.SHOP_ALI_PIC: {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else {
                    if (TypeConstant.CHANNEL_TYPE_NEWS.equals(this.type)) {
//                        if (TypeConstant.NEWS_UI_BIG_ONE_IMG.equals(news.getUiType())) {//新闻栏目的大图
                        if (TypeConstant.NEWS_UI_BIG_TWO_IMG.equals(news.getUiType())) {//新闻栏目的大图

                            return NEWS_BIG_IMAGE;
                        } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType()) && news.getArticleThumb().length >= 1 && news.getArticleThumb().length < 3) {
                            return NEWS_ONE_IMAGE;
                        } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                            return NEWS_THREE_IMAGE_WITH_NUM;
                        }
                    } else {
                        return PIC_BIG_IMAGE;
                    }
                }

            }
            //图文商品的
            case TypeConstant.SHOP_ALI: {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return NEWS_NO_IMAGE;
                } else {
                    if (TypeConstant.CHANNEL_TYPE_NEWS.equals(this.type)) {//新闻栏目的图文
                        if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType()) && news.getArticleThumb().length >= 1 && news.getArticleThumb().length < 3) {
                            return NEWS_ONE_IMAGE;
                        } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                            return NEWS_THREE_IMAGE;
                        }
                    }
                }
            }
            //适配所有视频
            case TypeConstant.VIDEO_ORIGINAL:
            case TypeConstant.VIDEO_NORMAL:
            case TypeConstant.EXCLUSIVE_NEWS_VIDEO:
            case TypeConstant.VIDEO_WEB: {
                StatService.onEvent(context, "article_exposed", "信息流_视频", 1);// 统计的点击的次数
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return DEFAULT;
                } else if (TypeConstant.NEWS_UI_SMALL_IMG.equals(news.getUiType())) {
                    return VIDEO_ONE_IMAGE;
                } else if (TypeConstant.NEWS_UI_BIG_TWO_IMG.equals(news.getUiType())
                        && TypeConstant.CHANNEL_TYPE_VIDEO.equals(this.type)) {
                    return VIDEO_BIG_IMAGE;
                } else {
                    return VIDEO_BIG_IMAGE_ONE;
                }
            }
            case TypeConstant.POST_2052://新版话题
            {
                if (TextUtils.equals(news.getUiType(), TypeConstant.NEWS_UI_SMALL_IMG)) {
                    return TOPIC_SMALL;
                } else if (TextUtils.equals(news.getUiType(), TypeConstant.NEWS_UI_BIG_ONE_IMG) ||
                        TextUtils.equals(news.getUiType(), TypeConstant.NEWS_UI_BIG_TWO_IMG)) {
                    return TOPIC_BIG;
                } else {
                    return DEFAULT;
                }
            }
            //适配所有广告类型
            case TypeConstant.AD: {
                if (news.getNativeAd() == null) {
                    break;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_GAME)) {
                    if (TextUtils.equals(type, TypeConstant.CHANNEL_TYPE_NEWS)) {
                        return GAMEN_PIC_NEWS;
                    } else if (TextUtils.equals(type, TypeConstant.CHANNEL_TYPE_PICS)) {
                        return GAMEN_PIC_PIC;
                    } else if (TextUtils.equals(type, TypeConstant.CHANNEL_TYPE_VIDEO)) {
                        return GAMEN_PIC_VIDEO;
                    }
                    break;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_TOUTIAO)) {
//                    if (TextUtils.equals(news.getNativeAd().getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_SMALL)) {
//                        return AD_TT_SMALL;
//                    } else if (TextUtils.equals(news.getNativeAd().getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_BIG)) {
//
//                    }
                    return AD_TT_BIG;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_BAIDU) &&
                        TextUtils.equals(news.getNativeAd().getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_SMALL)) {
                    return AD_SMALL_BAIDU;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_BAIDU) &&
                        TextUtils.equals(news.getNativeAd().getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_BIG)) {
                    return AD_BIG_BAIDU;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_GDT) &&
                        TextUtils.equals(news.getNativeAd().getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_BIG)) {
                    return AD_BIG_GDT;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_GDT) &&
                        TextUtils.equals(news.getNativeAd().getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_SMALL)) {
                    return AD_SMALL_GDT;
                } else if (TextUtils.equals(news.getNativeAd().getAdType(), AdConfig.AD_TYPE_H5)) {
                    //h5广告UI匹配
                    news.nativeToNews();//广告数据转换
                    if (news.getNativeAd().getThumbList() == null
                            || news.getNativeAd().getThumbList().length == 0) {
                        if (TextUtils.isEmpty(news.getArticleTitle())) {
                            return DEFAULT;
                        }
                        return NEWS_NO_IMAGE;
                    } else if (AdConfig.AD_VIEW_TYPE_LIST_NATIVE_SMALL.equals(news.getNativeAd().getStyle())) {
                        //小图
                        if (news.getArticleThumb().length >= 1 && news.getArticleThumb().length < 3) {
                            return NEWS_ONE_IMAGE;
                        } else if (news.getArticleThumb().length >= 3) {
                            return NEWS_THREE_IMAGE;
                        } else {
                            return NEWS_NO_IMAGE;
                        }
                    } else if (AdConfig.AD_VIEW_TYPE_LIST_NATIVE_BIG.equals(news.getNativeAd().getStyle())) {
                        //大图
                        return NEWS_BIG_IMAGE;
                    } else if (AdConfig.AD_VIEW_TYPE_LIST_NATIVE_THREE.equals(news.getNativeAd().getStyle())) {
                        //小图
                        if (news.getArticleThumb().length >= 1 && news.getArticleThumb().length < 3) {
                            return NEWS_ONE_IMAGE;
                        } else if (news.getArticleThumb().length >= 3) {
                            return NEWS_THREE_IMAGE;
                        } else {
                            return NEWS_NO_IMAGE;
                        }
                    }
                    return DEFAULT;
                } else {
                    return DEFAULT;
                }
            }
            case TypeConstant.GAME_VIDEO://游戏视频
            {
                if (news.getArticleThumb() == null || news.getArticleThumb().length == 0) {
                    return DEFAULT;
                } else if (TextUtils.equals(news.getShowPlayBtn(), "0")) {
                    //不展示视频播放按钮
                    if (TypeConstant.CHANNEL_TYPE_NEWS.equals(this.type)) {
                        return GAMEN_VIDEO_THREE;
                    } else if (TypeConstant.CHANNEL_TYPE_PICS.equals(this.type)) {
                        return GAMEN_VIDEO_FOUR;
                    } else if (TypeConstant.CHANNEL_TYPE_VIDEO.equals(this.type)) {
                        return GAMEN_VIDEO_TWO;
                    }
                } else if (TypeConstant.CHANNEL_TYPE_VIDEO.equals(this.type)) {
                    return GAMEN_VIDEO_TWO;
                } else if (TypeConstant.CHANNEL_TYPE_PICS.equals(this.type)) {
                    return GAMEN_VIDEO_FOUR;
                } else {
                    return GAMEN_VIDEO_ONE;
                }
            }

            case TypeConstant.GAME_RANKING://游戏排行
                if (rankList != null && rankList.size() > 0) {

                    return GAME_RANKING_VIEW;

                }

                return DEFAULT;
            //适配刷新
            case TypeConstant.FLUSH:
                return FLUSH;
            default:
                return DEFAULT;
        }


        return DEFAULT;
    }

    /**
     * 添加已读新闻
     *
     * @param newsid
     */
    public static void addReadList(String newsid) {
        if (readList == null) {
            readList = new ArrayList<>();
        }
        if (!TextUtils.isEmpty(newsid) && !readList.contains(newsid)) {
            readList.add(newsid);
//            String sql = "insert into "+ NewsDBSqlHelper.NEWS_SEE+" values ("+newsid+")";
//            NewsDBUtil.getInstance().changeData(sql);
        }
    }

    /**
     * 判断是否已读
     *
     * @param newsid
     * @return
     */
    public static boolean isReadNews(String newsid) {
        if (readList != null && readList.contains(newsid)) {
            return true;
        }
        return false;
    }

    private class ViewHolder1 implements View.OnClickListener {
        LinearLayout gameLayout;
        View divider;
        TextView rankName;
        TextView rankMore;

        public ViewHolder1(View contentView) {
            gameLayout = (LinearLayout) contentView.findViewById(R.id.ll_gamelayout);
            divider = contentView.findViewById(R.id.driver2);
            rankName = (TextView) contentView.findViewById(R.id.rank_list);
            rankMore = (TextView) contentView.findViewById(R.id.rank_more);
            divider.setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.rank_more).setOnClickListener(this);
            contentView.findViewById(R.id.rank_list).setOnClickListener(this);
        }

        public void loaddata(List<GameCenterEntity> list) {
            if (list != null && list.size() > 0) {

                if (gameLayout.getVisibility() == View.GONE) {
                    gameLayout.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        StatService.onEvent(context, "game_exposed", "最热游戏推荐", 1);//统计事件的点击次数
                        GameCenterEntity gameCenterEntity = list.get(i);
                        RanklGameView ranklGameView = new RanklGameView(context, gameCenterEntity, i);
                        ranklGameView.setTag(gameCenterEntity.getArticleId());
                        ranklGameView.setOnClickListener(this);
                        gameLayout.addView(ranklGameView);
                    }
                }
                if (!TextUtils.isEmpty(mRankName)) {
                    rankName.setText(mRankName);
                } else {
                    rankName.setText("");
                }
                rankMore.setVisibility(View.GONE);
            } else if (gameLayout.getVisibility() == View.VISIBLE) {
                gameLayout.setVisibility(View.GONE);
            }

        }

        @Override
        public void onClick(View v) {
            if (v == null || context == null) {
                return;
            }
            if (v.getTag() == null) {
                return;
            }
            StatService.onEvent(context, "067", "最热游戏推荐", 1);//统计事件的点击次数
            Intent intent = new Intent();
            intent.putExtra(Config.Extra.PARAMETER1, v.getTag().toString());
            intent.setClass(context, GameDetailActivity.class);
            context.startActivity(intent);
        }
    }

    private static class TouTiaoView {
        FrameLayout frameLayout;
        View viewLine;
    }

    private void initTouTiaoView(View view, TouTiaoView mHolder) {
        LogUtils.e("toutiao","initTouTiaoView------------------");
        mHolder.frameLayout = view.findViewById(R.id.view_express);
        mHolder.viewLine = view.findViewById(R.id.item_divider);
    }

    private View getTouTiaoView(View convertView, ViewGroup parent, int position) {
        final TouTiaoView holder;
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_toutiao_express_ad, parent, false);
                holder = new TouTiaoView();
                initTouTiaoView(convertView, holder);
                convertView.setTag(holder);
            } else {
                holder = (TouTiaoView) convertView.getTag();
            }
            initTouTiaoViewData(holder, position);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    private final SparseArray<TTNativeExpressAd> ttNativeExpressAdSparseArray = new SparseArray<>();
    private boolean flushToutiao;

    private void initTouTiaoViewData(TouTiaoView viewHolder, int position) {
        LogUtils.e("toutiao","initTouTiaoViewData------------------");
        if (viewHolder.frameLayout == null) return;
        NewsEntity newsEntity = list.get(position);
        if (newsEntity == null) return;
        viewHolder.viewLine.setVisibility(View.VISIBLE);
        viewHolder.frameLayout.setVisibility(View.VISIBLE);
        viewHolder.frameLayout.removeAllViews();
        NativeAd nativeAd = newsEntity.getNativeAd();
        if (nativeAd == null) return;
        int nativeAdSize = 0;
        // 如果没有获取到广告则 从广告管家里获取一个新广告
        ToutiaoNativeAd adManager = ToutiaoAdManager.getInstance().getAd(nativeAd.getAdId());
        if (adManager == null) {
            ToutiaoAdManager.getInstance().putAd(nativeAd);
        } else {
            nativeAdSize = adManager.getNativeAdSize();
        }
        if (!flushToutiao) {
            TTNativeExpressAd ttNativeExpressAd = ttNativeExpressAdSparseArray.get(position);
            if (ttNativeExpressAd != null) {
                fillToutiaoView(viewHolder, position, newsEntity, ttNativeExpressAd);
                return;
            }
        }
//        LogUtils.e("------------------------------------------------->" + position + " flushToutiao->" + flushToutiao);
        // 如果广告缓存中有广告则开始获取
        if (nativeAdSize > 0) {
            TTNativeExpressAd nrAd = adManager.getNativeAd();
            // 如果获取的广告不为null
            if (nrAd != null) {
                fillToutiaoView(viewHolder, position, newsEntity, nrAd);
            }
        } else {
            String ad = "";
//            if (AdConfigManagerNew.localeList != null && AdConfigManagerNew.localeList.size() > 0) {
//                for (NativeAd item : AdConfigManagerNew.localeList) {
//                    if (item != null && AdConfigManagerNew.TT_AD_TYPE.equals(item.getAdType())) {
//                        ad = item.getAdId();
//                        break;
//                    }
//                }
//            }
            //用获取到的广告id去获取预加载的广告
            if (!TextUtils.isEmpty(ad)) {
                ToutiaoNativeAd bna = ToutiaoAdManager.getInstance().getAd(ad);
                if (bna != null) {
                    TTNativeExpressAd oneNrAd = bna.getNativeAd();
                    // 如果获取的广告不为null
                    if (oneNrAd != null) {
                        fillToutiaoView(viewHolder, position, newsEntity, oneNrAd);
                        return;
                    }
                }
            }
            //如果最终还是没有获取到加载好的广告,则通过广告id去请求广告
            ToutiaoNativeAd bna = ToutiaoAdManager.getInstance().getAd(nativeAd.getAdId());
            if (bna != null) {
                bna.loadOneAd(context, nativeAd.getAdId(), new ToutiaoNativeAd.NativeAdLoadCallback() {

                    @Override
                    public void onNativeAdLoad(TTNativeExpressAd ad, boolean success) {
                        if (ad != null && success) {
                            fillToutiaoView(viewHolder, position, newsEntity, ad);
                        }
                    }
                });
            }
        }

    }

    private void fillToutiaoView(TouTiaoView viewHolder, int position, NewsEntity newsEntity, TTNativeExpressAd nrAd) {
        if (newsEntity == null || nrAd == null) return;
        ttNativeExpressAdSparseArray.put(position, nrAd);
        delDialogView(viewHolder, newsEntity, nrAd, position);
        View expressAdView = nrAd.getExpressAdView();
        if (expressAdView != null && expressAdView.getParent() == null) {
            viewHolder.frameLayout.addView(expressAdView);
//            nrAd.render();
        }
    }

    private void delDialogView(TouTiaoView viewHolder, NewsEntity newsEntity, TTNativeExpressAd adView, int position) {
        //使用自定义样式
        List<FilterWord> words = adView.getFilterWords();
        if (words == null || words.isEmpty()) {
            return;
        }
        ToutiaoCloseDialog toutiaoCloseDialog = new ToutiaoCloseDialog(context, words);
        toutiaoCloseDialog.setOnDislikeItemClick(new ToutiaoCloseDialog.OnDislikeItemClick() {
            @Override
            public void onItemClick(FilterWord filterWord) {
                LogUtils.e("toutiao filterWord", filterWord.getName(), "del position->" + position);
//                LiveEventBus.get(Config.EventBus.delNewsListAd, Integer.class).post(position);
//                list.remove(newsEntity);
//                notifyDataSetChanged();
                viewHolder.frameLayout.setVisibility(View.GONE);
            }
        });
        toutiaoCloseDialog.setCancelable(true);
        toutiaoCloseDialog.setCanceledOnTouchOutside(true);
        adView.setDislikeDialog(toutiaoCloseDialog);
    }
}
